package cn.itsource.pay.service.impl;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.order.service.IProductOrderService;
import cn.itsource.pay.domain.AlipayInfo;
import cn.itsource.pay.domain.PayBill;
import cn.itsource.pay.mapper.AlipayInfoMapper;
import cn.itsource.pay.mapper.PayBillMapper;
import cn.itsource.pay.service.IPayBillService;
import cn.itsource.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Year;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill bill) {
        // 支付校验
        if (bill == null) {
            throw new BusinessException("请创建支付单再进行支付！！");
        }
        PayBill billTmp = payBillMapper.loadByUnionPaySn(bill.getUnionPaySn());
        if (billTmp == null) {
            throw new BusinessException("请创建支付单再进行支付！！");
        }
        switch(bill.getPayChannel().intValue()){//0 余额 1 支付宝 2 微信 3 银联
            case 1 ://1 支付宝
                AlipayInfo info = alipayInfoMapper.loadByShopId(bill.getShop_id());
                return AlipayUtils.pay(info, bill);
            case 2 ://2 微信 TODO
                break;
            case 3 ://3 银联 TODO
                break;
            default : //0  余额  TODO
                //语句
        }
        return null;
    }

    public PayBill loadByUnionPaySn(String unionPaySn){
       return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
